﻿using System;
using mBlog.Data.Entities;
using AutoMapper;
using mBlog.ViewModels;
using mBlog.Admin.ViewModels;
using System.Web.Mvc;
using System.Web;

namespace mBlog.Infrastructure.AutoMapper
{
    public partial class AutoMapperConfiguration
    {
        protected UrlHelper UrlHelper
        {
            get
            {
                return new UrlHelper(HttpContext.Current.Request.RequestContext);
            }
        }

        public void Init()
        {
            //mapping from entity to model
            Mapper.CreateMap<Comment, Models.Comment>();
            Mapper.CreateMap<Category, Models.Category>()
                .ForMember(c => c.Posts, opt => opt.Ignore());
            Mapper.CreateMap<Tag, Models.Tag>()
                .ForMember(t => t.ID, opt => opt.MapFrom(t => t.Id))
                .ForMember(t => t.Posts, opt => opt.Ignore());

            Mapper.CreateMap<Post, Models.Post>();

            //models to entities
            Mapper.CreateMap<Models.Comment, Comment>();
            Mapper.CreateMap<Models.Tag, Tag>()
                .ForMember(t => t.Posts, opt => opt.Ignore());
            Mapper.CreateMap<Models.Category, Category>()
                .ForMember(p => p.Posts, opt => opt.Ignore());

            //only mapping post content
            Mapper.CreateMap<Models.Post, Post>()
                .ForMember(c => c.Comments, opt => opt.Ignore())
                .ForMember(c => c.Categories, opt => opt.Ignore())
                .ForMember(c => c.Tags, opt => opt.Ignore());

            Mapper.CreateMap<CommentInput, Comment>();
            Mapper.CreateMap<PostInput, Post>()
                .ForMember(p => p.Tags, o => o.MapFrom(i => TagResolver.ResolveTags(i.Tags)))
                .ForMember(p => p.Categories, o => o.MapFrom(p => CategoryResolver.ResolveCategories(p.CategoryUrlName)));

            Mapper.CreateMap<Models.Post, PostInput>()
                .ForMember(input => input.CategoryUrlName, o => o.MapFrom(p => CategoryResolver.ResolveCategoryName(p.Categories)))
                .ForMember(input => input.Tags, o => o.MapFrom(p => TagResolver.ResolveTagStrings(p.Tags)));

            Mapper.CreateMap<Models.Post, PostDetail>()
                .ForMember(detail => detail.Id, o => o.MapFrom(p => p.ID))
                .ForMember(detail => detail.Url,
                           o => o.MapFrom(p => UrlHelper.Action("View", "Post", new { urlName = p.UrlName })))
                .ForMember(detail => detail.Content, o => o.MapFrom(p => MvcHtmlString.Create(p.Content)))
                .ForMember(detail => detail.PublishedAt,
                           o =>
                           o.MapFrom(
                               p => p.PublishedTime.HasValue ? p.PublishedTime.Value.ToShortDateString() : string.Empty))
                .ForMember(detail => detail.Title, o => o.MapFrom(p => MvcHtmlString.Create(p.Title)));

            Mapper.CreateMap<Models.Comment, CommentDetail>()
                .ForMember(detail => detail.Id,
                           o => o.MapFrom(c => c.ID))
                .ForMember(detail => detail.Content,
                           o => o.MapFrom(c => MvcHtmlString.Create(c.Content)))
                .ForMember(detail => detail.CreatedAt,
                           o => o.MapFrom(c => c.CommentTime.ToUniversalTime().AddHours(8).ToString()));

            Mapper.CreateMap<Models.Tag, TagDetail>()
                .ForMember(td => td.Name, o => o.MapFrom(t => t.Name))
                .ForMember(td => td.Url, o => o.MapFrom(t => UrlHelper.Action("View", "Tag", new { urlName = t.UrlName })))
                .ForMember(td => td.Id, o => o.MapFrom(t => t.ID));

            Mapper.CreateMap<Models.Category, CategoryDetail>()
                .ForMember(cd => cd.Name, o => o.MapFrom(t => t.Name))
                .ForMember(cd => cd.Id, o => o.MapFrom(t => t.ID))
                .ForMember(cd => cd.Url,
                           o => o.MapFrom(t => UrlHelper.Action("View", "Category", new { urlName = t.UrlName })));

            Mapper.CreateMap<CategoryInput, Models.Category>()
                .ForMember(c => c.Posts, o => o.Ignore())
                .ForMember(c => c.AddedTime, o => o.MapFrom(p => DateTime.Now));
        }
    }
}
